package com.hopu.oa.controller.plan;

import com.hopu.oa.dao.plan.AoaPlanListDao;
import com.hopu.oa.entity.dto.user.UserInformation;
import com.hopu.oa.entity.management.AoaStatusList;
import com.hopu.oa.entity.management.AoaTypeList;
import com.hopu.oa.entity.plan.AoaPlanList;
import com.hopu.oa.entity.process.AoaAttachmentList;
import com.hopu.oa.entity.user.AoaUser;
import com.hopu.oa.service.plan.AoaPlanListService;
import com.hopu.oa.until.OosUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Controller
public class planManageController {

    private static Integer typet = null;
    private static Integer statust = null;
    private static Integer timet = null;

    private static Integer pidt = null;

    @Autowired
    private AoaPlanListService aoaPlanListService;

    @Autowired
    private AoaPlanListDao aoaPlanListDao;


    // oos 工具类   -> DI ,用来更新头像
    @Autowired
    private OosUtil oosUtil;

    /**
     * @param baseKey
     * @param model
     * @param page
     * @param size
     * @param session
     * @return
     */
    @RequestMapping("planview")
    public String selectAll(String baseKey, Model model, @RequestParam(value = "page", defaultValue = "1") int page, @RequestParam(value = "size", defaultValue = "3") int size, HttpSession session) {
        Integer userId = ((AoaUser) session.getAttribute("user")).getUserId();
        //总数据
        int totalElements = aoaPlanListService.selectAll(userId, baseKey, page, 0, null, null, null).size();
        //有几页
        int totalPages = totalElements % size == 0 ? totalElements / size : totalElements / size + 1;
        List<AoaPlanList> list = aoaPlanListService.selectAll(userId, baseKey, (page - 1) * size, size, null, null, 0);

        HashMap<String, Integer> map = new HashMap<>();
        map.put("totalElements", totalElements);
        map.put("totalPages", totalPages);
        map.put("number", page);
        map.put("size", size);

        model.addAttribute("baseKey", baseKey);
        model.addAttribute("page", map);
        model.addAttribute("plist", list);
        model.addAttribute("url", "planDis");
        return "plan/planview";
    }

    /**
     * 内层分页
     *
     * @param baseKey
     * @param model
     * @param page
     * @param size
     * @param session
     * @param request
     * @return
     */
    @RequestMapping("/planDis")
    public String sellectPlan(String baseKey, Model model, @RequestParam(value = "page", defaultValue = "1") int page, @RequestParam(value = "size", defaultValue = "3") int size, HttpSession session, HttpServletRequest request) {
        Integer userId = ((AoaUser) session.getAttribute("user")).getUserId();
        String time = request.getParameter("time");
        String type = request.getParameter("type");
        String status = request.getParameter("status");
        String icon = request.getParameter("icon");
        model.addAttribute("icon", icon);
        if (time != null) {
            timet = Integer.parseInt(time);
            typet = null;
            statust = null;
            model.addAttribute("time", timet);
        }
        if (type != null) {
            timet = null;
            typet = Integer.parseInt(type);
            statust = null;
            model.addAttribute("type", typet);
        }
        if (status != null) {
            timet = null;
            typet = null;
            statust = Integer.parseInt(status);
            model.addAttribute("status", statust);
        }
        //总数据
        int totalElements = aoaPlanListService.selectAll(userId, baseKey, page, 0, null, null, null).size();
        //有几页
        int totalPages = totalElements % size == 0 ? totalElements / size : totalElements / size + 1;
        List<AoaPlanList> list = aoaPlanListService.selectAll(userId, baseKey, (page - 1) * size, size, typet, statust, timet);
        HashMap<String, Integer> map = new HashMap<>();
        map.put("totalElements", totalElements);
        map.put("totalPages", totalPages);
        map.put("number", page);
        map.put("size", size);
        model.addAttribute("baseKey", baseKey);
        model.addAttribute("page", map);
        model.addAttribute("plist", list);
        model.addAttribute("url", "planDis");
        return "plan/planviewtable";

    }

    //界面
    @RequestMapping("/planedit")
    public String temp(Model model, HttpServletRequest request, HttpSession session) {
        List<AoaStatusList> aoaStatusLists = aoaPlanListDao.selectStatusList();
        List<AoaTypeList> aoaTypeLists = aoaPlanListDao.selectTypeList();
        pidt = Integer.valueOf(request.getParameter("pid"));

        if (pidt > 0) {
            AoaPlanList aoaPlanList1 = aoaPlanListDao.selectByPid(pidt);
            model.addAttribute("plan", aoaPlanList1);
        }
        model.addAttribute("statuslist", aoaStatusLists);
        model.addAttribute("typelist", aoaTypeLists);
        return "plan/planedit";
    }

    /**
     * 添加和修改
     *
     * @param model
     * @param aoaPlanList
     * @param request
     * @return
     */
    @RequestMapping("/plansave")
    public String addAndUpdate(AoaPlanList aoaPlanList, HttpServletRequest request, HttpSession session, UserInformation userInformation, @RequestParam("file") MultipartFile file) {
        String type = request.getParameter("type");
        String status = request.getParameter("status");
        String page = request.getParameter("page");
        if (page == null) {
            page = "1";
        }
        //获取userId
        Integer userId = ((AoaUser) session.getAttribute("user")).getUserId();
        //获取系统当前时间
        Date date = new Date();
        String format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
        aoaPlanList.setCreateTime(format);

        //DTO user
        UserInformation dtoUser = userInformation;

        if (type != null && status != null) {

            //获取type_id
            AoaTypeList aoaTypeList = aoaPlanListDao.selectTypeId(type);
            //文件
            AoaAttachmentList aoaAttachmentList = new AoaAttachmentList();
            Integer typeId = aoaTypeList.getTypeId();
            aoaPlanList.setTypeId(typeId);
            //获取status_id
            AoaStatusList aoaStatusList = aoaPlanListDao.selectStatusId(status);
            Integer statusId = aoaStatusList.getStatusId();
            aoaPlanList.setStatusId(statusId);

            //判断是添加还是修改
            if (pidt < 0) {

                //添加
                aoaPlanList.setPlanUserId(userId);
                //文件上传

                if (null != file) {
                    String filename = file.getOriginalFilename();
                    String contentType = file.getContentType();
                    long size = file.getSize();

                    if (file.getSize() != 0) {
                        //上传到OSS
                        dtoUser.setImgPath(oosUtil.getImgUrl(oosUtil.uploadImg2Oss(file)));

                        aoaAttachmentList.setAttachmentName(filename);
                        aoaAttachmentList.setAttachmentPath(oosUtil.getImgUrl(oosUtil.uploadImg2Oss(file)));
                        aoaAttachmentList.setAttachmentSize(size + "");
                        aoaAttachmentList.setAttachmentType(contentType);
                        aoaAttachmentList.setUserId(userId + "");
                        aoaAttachmentList.setModel("aoa_plan_list");
                        aoaAttachmentList.setUploadTime(format);
                    }
                }
                aoaPlanListService.insert(aoaAttachmentList, aoaPlanList);
                return "redirect:planview";
            } else {
                //修改
                aoaPlanList.setPlanId(pidt);
                aoaPlanList.setPlanUserId(userId);
                //文件修改
                if (null != file) {
                    String filename = file.getOriginalFilename();
                    String contentType = file.getContentType();
                    long size = file.getSize();
                    if (file.getSize() != 0) {
                        //上传到OSS
                        dtoUser.setImgPath(oosUtil.getImgUrl(oosUtil.uploadImg2Oss(file)));
                        aoaAttachmentList.setAttachmentName(filename);
                        aoaAttachmentList.setAttachmentPath(oosUtil.getUrl(oosUtil.uploadImg2Oss(file)));
                        aoaAttachmentList.setAttachmentSize(size + "");
                        aoaAttachmentList.setAttachmentType(contentType);
                        aoaAttachmentList.setUserId(userId + "");
                        aoaAttachmentList.setModel("aoa_plan_list");
                        aoaAttachmentList.setUploadTime(format);
                    }
                }
                aoaPlanListService.insertUpdate(aoaAttachmentList, aoaPlanList);
                return "redirect:planview?page=" + page;
            }
        }
        return "plan/planview";
    }


    /**
     * 删除
     *
     * @param request
     * @return
     */
    @RequestMapping("/plandelete")
    public String delete(HttpServletRequest request) {
        String page = request.getParameter("page");
        String pid = request.getParameter("pid");
        if (page == null) {
            page = "1";
        }
        aoaPlanListDao.deleteById(pid);
        return "redirect:planview?page=" + page;
    }


}
